﻿Imports System.Text
Imports SISSAT.BusinessLayer.Services
Imports SISSAT.BusinessLayer.DomainModels

Namespace Utilities
    Public Class EmployeeCodeHelper

        ''' <summary>
        ''' Generates a unique Student ID or Instructors ID based on the  
        ''' school year, semester, course and a 3 digit random number
        ''' </summary>
        ''' <param name="courseId">Course ID</param>
        ''' <param name="employeeYear">Employees years of stay in the school</param>
        ''' <param name="schoolYear">Current school year</param>
        ''' <param name="semester">Current semester of the school year</param>
        ''' <returns>A unique random 10 digit number</returns>
        ''' <remarks></remarks>
        Public Shared Function GenerateEmployeeCode(courseId As Integer, _
                                                    employeeYear As Integer, _
                                                    schoolYear As Integer, _
                                                    semester As Semesters) As String
            Dim service As IEmployeeService = New EmployeeService
            Dim uniqueNumber As Integer = service.ReadForEmployeeCode(courseId, employeeYear)

            Dim course As String = Convert.ToString(courseId + 10)

            uniqueNumber = Convert.ToInt32(IIf(uniqueNumber = 0, 1, uniqueNumber))
            Dim uniqueCode As String = Convert.ToString(uniqueNumber)
            Dim count As Integer = uniqueCode.Length

            If count < 3 Then
                Dim numberFormat As String = "0{0}"
                For counter As Integer = 1 To 2
                    uniqueCode = String.Format(numberFormat, uniqueCode)
                Next
            End If

            Dim employeeCode As StringBuilder = New StringBuilder
            employeeCode.Append(DateTime.Now.Year)
            employeeCode.Append(semester)
            employeeCode.Append(course)
            employeeCode.Append(uniqueCode)

            Return employeeCode.ToString()
        End Function

    End Class
End Namespace

